﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InvokeBeginInvoke
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void TestInvoke()
        {
            listBox1.Items.Add("--begin--");
            var bi = listBox1.Invoke(new Action(() =>
            {
                //Thread.Sleep(10000);
            listBox1.Items.Add("Invoke " + Thread.CurrentThread.Name);
            }));
            Thread.Sleep(1000);
            listBox1.Items.Add("--end--");
        }
        private void TestBeginInvoke()
        {
            listBox1.Items.Add("--begin--");
            var bi = listBox1.BeginInvoke(new Action(() =>
            {
                //Thread.Sleep(10000);
                listBox1.Items.Add("BeginInvoke " + Thread.CurrentThread.Name);
            }));
            Thread.Sleep(1000);
            listBox1.Items.Add("--end--");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            TestBeginInvoke();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TestInvoke();
        }
    }
}
